package com.xiaoq.matrix.endpoint;


import java.util.List;

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.matrix.entity.ContentEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(tags={"解决七牛云图片访问问题"})
@RestController
@RequestMapping("/api/v1/solution")
public class QiniuSolutionEndpoint {
	
	@Autowired
	private Datastore datastore;
	
	@ApiOperation(value = "替换七牛云头条内容图片", notes = "替换七牛云头条内容图片")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericDataResponse.class, message = "替换七牛云头条内容图片") })
	@PutMapping(value = "", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public void updateContentImage(){
		Query<ContentEntity> contentQuery = datastore.createQuery(ContentEntity.class);
		List<ContentEntity> contents = contentQuery.asList();
		for(ContentEntity content:contents){
			
			String imgUrl = content.getImgUrl();
			if(!StringUtils.isEmpty(imgUrl)){
				imgUrl = myReplace(imgUrl);
				content.setImgUrl(imgUrl);
			}
			String staticPath = content.getStaticPath();
			if(!StringUtils.isEmpty(staticPath)){
				staticPath = myReplace(staticPath);
				content.setStaticPath(staticPath);
			}
			
			String detailInfo = content.getDetailInfo();
			if(!StringUtils.isEmpty(detailInfo)){
				detailInfo = myReplace(detailInfo);
				content.setDetailInfo(detailInfo);
				
			}
			datastore.save(content);
		}
		

	}
	
	
	
	public String myReplace(String secense){
		
			return secense.replace("p3cif3r0q.bkt.clouddn.com", "qiniurepository.xiaoq.online");
		
	}

}
